package com.winit.schedule.job.tracker.web.controller;

import com.alibaba.fastjson.JSON;
import com.winit.schedule.core.repository.entity.TaskEntity;
import com.winit.schedule.core.repository.entity.WhiteListEntity;
import com.winit.schedule.core.repository.query.TaskQuery;
import com.winit.schedule.core.repository.query.WhiteListQuery;
import com.winit.schedule.core.zk.ZKService;
import com.winit.schedule.job.tracker.JobTrackerContext;
import com.winit.schedule.job.tracker.repository.ConsoleDataManager;
import com.winit.schedule.webc.annotation.Controller;
import com.winit.schedule.webc.annotation.RequestMapping;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by jianke.zhang on 2017/3/21.
 */
@Controller(OpenController.NAMESPACE)
public class OpenController extends BaseController{

    public static final String NAMESPACE = "open";

    private static Logger logger = LoggerFactory.getLogger(OpenController.class);

    private static OpenController instance = null;

    private ConsoleDataManager consoleDataManager;

    private JobTrackerContext jobTrackerContext;

    private OpenController(JobTrackerContext jobTrackerContext){
        this.jobTrackerContext = jobTrackerContext;
        this.consoleDataManager = jobTrackerContext.getConsoleDataManager();
    }

    public static synchronized OpenController getInstance(JobTrackerContext context) {
        if (null == instance) {
            instance = new OpenController(context);
        }
        return instance;
    }

    @RequestMapping("resetBlackList")
    public Map<String, Object> resetBlackList(HttpServletRequest req, HttpServletResponse resp) {
        String ipaddress = req.getParameter("ip");
        String errorMsg = null;
        try {
            boolean hasAuth = hasAuth(req);
            if (hasAuth) {
                ZKService zkService = jobTrackerContext.getZkService();
                zkService.resetBlackList(ipaddress);
            } else {
                errorMsg = "没有权限";
            }
        } catch (Exception e) {
            errorMsg = "操作失败";
            logger.error("添加白名单失败: " + ipaddress, e);
        }
        return buildMesssage(errorMsg);
    }

    private boolean hasAuth(HttpServletRequest req) {
        return true;
    }

    private static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


}
